package com.otp.service.instance.impl;

import java.util.List;

import org.appfuse.model.Organization;
import org.appfuse.service.impl.GenericManagerImpl;
import org.springframework.beans.factory.annotation.Autowired;

import com.otp.model.test.instance.TestInstance;
import com.otp.model.test.instance.TestInstanceScore;
import com.otp.model.test.instance.TestItemResponseInstance;
import com.otp.model.test.instance.dao.TestInstanceDao;
import com.otp.model.test.template.TestTemplate;
import com.otp.service.instance.TestInstanceManager;

public class TestInstanceManagerImpl extends GenericManagerImpl<TestInstance,Long> implements TestInstanceManager {
	private TestInstanceDao testInstanceDao;
	
	@Autowired
	public void setTestInstanceDao(TestInstanceDao testInstanceDao){
		this.dao = testInstanceDao;
        this.testInstanceDao = testInstanceDao;
	}
	
	public List<TestInstance> getTestInstanceByUser(String userId,String templateId) {
		List<TestInstance> instances = null;
		try {
			instances = testInstanceDao.getTestInstanceByUser(userId,templateId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return instances;
	}

	public TestInstance getTestInstance(String instanceId) {
		return testInstanceDao.get(new Long(instanceId));
	}

	public TestInstance saveTestInstance(TestInstance testInstance) {
		return testInstanceDao.save(testInstance);
	}

	public void revomeInstance(String testInstanceId) {
		testInstanceDao.remove(new Long(testInstanceId));
		
	}

	public List<TestTemplate> getSubTests(String templateId) {
		List<TestTemplate> subTests = null;
		try {
			subTests = testInstanceDao.getSubTests(templateId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subTests;
	}

	public void save(TestInstanceScore testInstanceScore) {
		try {
			testInstanceDao.save(testInstanceScore);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public TestTemplate fetchTemplate(String templateId) {
		TestTemplate template = null;
		try {
			template = testInstanceDao.fetchTemplate(templateId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return template;
	}
	
	public TestTemplate fetchTemplateByName(String name) {
		TestTemplate template = null;
		try {
			template = testInstanceDao.fetchTemplateByName(name);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return template;
	}

	public List<TestInstanceScore> getTestInstanceScore(String testInstanceId) {
		List<TestInstanceScore> returnedScores = null;
		try {
			returnedScores = testInstanceDao.getTestInstanceScore(testInstanceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnedScores;
	}	
		
	public TestInstanceScore getSingleTestInstanceScore(String testTemplateId, String testInstanceId){
		TestInstanceScore returnScore = null;
		try {
			returnScore = testInstanceDao.getSingleTestInstanceScore(testInstanceId, testTemplateId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnScore;
	}

	public List<TestItemResponseInstance> getTestItemResponseInstances(
			String testInstanceId) {
		List<TestItemResponseInstance> returnedItemResponse = null;
		try {
			returnedItemResponse = testInstanceDao.getTestItemResponseInstances(testInstanceId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnedItemResponse;
	}

	public List<TestItemResponseInstance> getTestItemResponseInstancesByInstanceIdAndTemplateId(
			String testInstanceId, String testTemplateId) {
		List<TestItemResponseInstance> returnedItemResponse = null;
		try {
			returnedItemResponse = testInstanceDao.getTestItemResponseInstancesByInstanceIdAndTemplateId(testInstanceId, testTemplateId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return returnedItemResponse;
	}
	
	public List<Organization> getAllOrganizations() {
		List<Organization> allOrgs = null;
		try {
			allOrgs = testInstanceDao.getAllOrganizations();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allOrgs;
	}

	public List<TestInstance> getAllTestInstances() {
		List<TestInstance> allInstances = null;
		try {
			allInstances = testInstanceDao.getAllTestInstances();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return allInstances;
	}

	public Organization getOrgByName(String orgName){
		Organization org = null;
		try {
			org = testInstanceDao.getOrgByName(orgName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return org;
	}
	
	public List<TestTemplate> getTemplateByOrganization(String organizationId) {
		List<TestTemplate> templates = null;
		try {
			templates = testInstanceDao.getTemplateByOrganization(organizationId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return templates;
	}
	
	public List<TestInstance>getTestInstancesByTemplateAndOrganization(String testTemplateId, String orgId){
		List<TestInstance> testInstances = null;
		try{
			testInstances=testInstanceDao.getTestInstancesByTemplateAndOrganization(testTemplateId, orgId);
		} catch (Exception e){
			e.printStackTrace();
		}
		return testInstances;
	}
	
	public List<TestInstance>getTestInstancesByTemplate(String testTemplateId){
		List<TestInstance> testInstances = null;
		try{
			testInstances=testInstanceDao.getTestInstancesByTemplate(testTemplateId);
		} catch (Exception e){
			e.printStackTrace();
		}
		return testInstances;
	}
	
	public List<TestInstance>getTestInstancesByOrganization(String orgId){
		List<TestInstance> testInstances = null;
		try{
			testInstances=testInstanceDao.getTestInstancesByOrganization(orgId);
		} catch (Exception e){
			e.printStackTrace();
		}
		return testInstances;
	}

}

